package com.dp.appi.dao.impl;

import com.dp.appi.dao.OrgUserDao;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by bchen on 2016/5/21.
 */
@Component
public class OrgUserDaoImpl extends BaseDaoImpl implements OrgUserDao {

    /**
     * 获取单位列表
     *
     * @param org_type
     * @return
     */
    @Override
    public List<Map<String, Object>> orgList(int org_type) {
        StringBuffer sql = new StringBuffer();
        sql.append("select id,departname,description,org_type ");
        sql.append("from t_s_depart  ");
        sql.append("where org_type = ?");
        return getJdbcTemplate().queryForList(sql.toString(),org_type);
    }

    /**
     * 获取人员列表
     *
     * @param design_unit
     * @param user_type
     * @return
     */
    @Override
    public List<Map<String, Object>> userByTypeList(String design_unit, String user_type,String userId) {

        StringBuffer sql = new StringBuffer();
        sql.append("select a.id,a.username,a.realname,c.departname   ");
        sql.append("from t_s_base_user a ");
        sql.append("INNER JOIN t_s_user_org b ");
        sql.append("on a.ID = b.user_id ");
        sql.append("INNER JOIN t_s_depart c ");
        sql.append("on c.ID = b.org_id ");
        sql.append("where a.ID = ? or (c.ID = ? and c.org_type = 12 ");
        sql.append("and a.usertype like ?) ");
        return getJdbcTemplate().queryForList(sql.toString(),userId,design_unit,"%"+user_type+"%");
    }

    /**
     * 根据用户名获取组织机构
     *
     * @param username
     * @return
     */
    @Override
    public Map<String, Object> getOrgByUsername(String username) {
        StringBuffer sql = new StringBuffer();
        sql.append("select c.id,c.departname,c.description,c.org_type from t_s_base_user a ");
        sql.append("INNER JOIN t_s_user_org b ");
        sql.append("on a.ID = b.user_id ");
        sql.append("INNER JOIN t_s_depart c ");
        sql.append("on c.ID = b.org_id ");
        sql.append("where a.username = ?");
        return queryForMap(sql.toString(),username);
    }

    /**
     * 获取当前用户信息
     *
     * @param username
     * @return
     */
    @Override
    public List<Map<String, Object>> userByUsernameList(String username,String userstageId) {
        StringBuffer sql = new StringBuffer();

        sql.append("select a.id,a.username,c.departname,a.realname ");
        sql.append("from t_s_base_user a ");
        sql.append("INNER JOIN t_s_user_org b ");
        sql.append("on a.ID = b.user_id ");
        sql.append("INNER JOIN t_s_depart c ");
        sql.append("on c.ID = b.org_id ");

        sql.append("where a.username = ? ");
        if(!userstageId.equals(""))
        {
            sql.append(" or  INSTR(?,a.ID)>0 ");
            return getJdbcTemplate().queryForList(sql.toString(),username,userstageId);
        }
        else
        {
            return getJdbcTemplate().queryForList(sql.toString(),username);
        }

    }
    /**
     * 获取用户信息
     *
     * @param userPhone
     * @return
     */
    @Override
    public List<Map<String, Object>> userByUserPhoneList(String userPhone) {
        StringBuffer sql = new StringBuffer();

        sql.append("select a.id,a.username,c.departname,a.realname ");
        sql.append("from t_s_base_user a ");
        sql.append("INNER JOIN t_s_user_org b ");
        sql.append("on a.ID = b.user_id ");
        sql.append("INNER JOIN t_s_depart c ");
        sql.append("on c.ID = b.org_id ");
        sql.append("INNER JOIN t_s_user d ");
        sql.append("on d.ID = a.id ");
        sql.append("where d.mobilePhone = ?  and b.org_id='297e640454cbba690154cd8eb0460010' ");
        return getJdbcTemplate().queryForList(sql.toString(),userPhone);
    }

    /**
     * 获取单位子级列表
     *
     * @param orgID
     * @param orgType
     * @return
     */
    @Override
    public List<Map<String, Object>> orgChildList(String orgID, String orgType) {
        StringBuffer sql = new StringBuffer();
        sql.append("select * From t_s_depart  ");
        sql.append("where FIND_IN_SET(ID,  ");
        sql.append("queryOrgChildrenList(?)) ");
        sql.append("and org_type = ? ");
        return getJdbcTemplate().queryForList(sql.toString(),orgID,orgType);
    }
}
